package com.beta.service.pay;

import com.alibaba.fastjson.JSON;
import com.beta.dto.pay.PayRequestDto;
import com.beta.em.pay.PayRoutes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by vector on 2016/11/25.
 */
@Service
public class PayRouteService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StrategyContext strategyContext;

    public String getPayReturnString(PayRequestDto payRequest) {
        logger.info("支付接口请求开始 <== pay_routes={}, request_info={}", payRequest.getPayRoutes(), JSON.toJSONString(payRequest));
        PayStrategy payStrategy = strategyContext.getPayStrategy(payRequest.getPayRoutes());
        long start = System.currentTimeMillis();
        String payResultsString = payStrategy.generatePayParams(payRequest.getPayRoutes(), payRequest);
        logger.info("支付接口请求结束 ==> 耗时={}, 返回结果={}", System.currentTimeMillis() - start, payResultsString);
        return payResultsString;
    }

    public Long getPayExpireMinutes(PayRoutes payRoutes) {
        PayStrategy payStrategy = strategyContext.getPayStrategy(payRoutes);
        return payStrategy.getPayExpireMinutes(payRoutes);
    }

    public BigDecimal getTradeFeeRate(PayRoutes payRoutes) {
        PayStrategy payStrategy = strategyContext.getPayStrategy(payRoutes);
        return payStrategy.getPayFeeRate(payRoutes);
    }

}
